<template>
  <div>
    <h1 style="margin: 30px auto; text-align: center;">管理系统员工新增</h1>
    <div class="png">
      <img src="../../assets/logo2.png">
    </div>
    <h2>Managesystem</h2>
    <el-form style="width: 600px;height: 1000px;margin: 0 auto" label-width="80px"
             :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
      <el-form-item label="员工姓名" prop="username">
        <el-input type="text" v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="员工密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="员工昵称" prop="nickname">
        <el-input type="text" v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="员工电话" prop="phone">
        <el-input type="text" v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="员工岗位" prop="post">
        <el-select  style="margin-right: 300px" v-model="ruleForm.post" placeholder="请选择员工岗位">
          <el-option label="总经理" value="总经理"></el-option>
          <el-option label="副总经理" value="副总经理"></el-option>
          <el-option label="人力资源" value="人力资源"></el-option>
          <el-option label="仓库管理" value="仓库管理"></el-option>
          <el-option label="财务管理" value="财务管理"></el-option>
          <el-option label="销售" value="销售"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工部门" prop="department">
        <el-select  style="margin-right: 300px" v-model="ruleForm.department" placeholder="请选择员工部门">
          <el-option label="办公室" value="办公室"></el-option>
          <el-option label="销售部" value="销售部"></el-option>
          <el-option label="财务部" value="财务部"></el-option>
          <el-option label="仓库部" value="仓库部"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工类型" prop="employeeType">
        <el-select style="margin-right: 300px" v-model="ruleForm.employeeType" placeholder="请选择员工类型">
          <el-option label="1" value="1"></el-option>
          <el-option label="2" value="2"></el-option>
          <el-option label="3" value="3"></el-option>
          <el-option label="4" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间" prop="employmentDate">
        <el-col :span="11">
          <div class="block">
            <el-date-picker
                v-model="ruleForm.employmentDate"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-col>
      </el-form-item>
      <el-form-item label="简介" prop="profile">
        <el-input type="text" v-model="ruleForm.profile"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button>
          <a href="/personManage/UserList">用户列表</a>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style>
  body {
    background-repeat: no-repeat;
    text-align: center;
  }
  h1{
    font-size: 72px;
    color: black;
    margin-bottom: 0;
  }
  h2{
    font-size: 30px;
    color: black;
    margin-bottom: 10px;
  }
  png{
    width: 100px;
    padding-top: 100px;
  }
  a{
    text-decoration: none;
    color: #2c3e50;
  }
</style>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: '123456',
        nickname: '老马',
        phone: '13888888888',
        post: '',
        department: "",
        employeeType: '',
        employmentDate: '',
        profile: "这个人很懒，什么也没说",
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '用户名长度必须在 2 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '密码长度必须在 4 到 15 个字符', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 1, max: 6, message: '用户名长度必须在 1 到 6 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { min: 8, max: 11, message: '电话号码长度必须在 8 到 11 个字符', trigger: 'blur' }
        ],
        post: [
          { required: true, message: '请选择岗位', trigger: 'blur' },
          { message: '岗位不能为空', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '请选择部门', trigger: 'blur' },
          { message: '部门不能为空', trigger: 'blur' }
        ],
        employeeType: [
          { required: true, message: '请选择员工类型', trigger: 'blur' },
          { message: '员工类型不能为空', trigger: 'blur' }
        ],
        employmentDate: [
          { required: true, message: '请选择入职日期', trigger: 'blur' },
        ],
        profile: [
          { required: true, message: '请输入个人简介', trigger: 'blur' },
          { min: 2, max: 200, message: '个人简介不能为空', trigger: 'blur' }
        ]
      }

    }
  },
    methods:{
      submitForm(formName) {
        //发出注册请求
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = 'http://localhost:8080/user/reg';
            console.log('url = ' + url);

            let formData = this.qs.stringify(this.ruleForm);
            console.log('formData = ' + formData)
            
            this.axios.post(url, formData).then((response) => {
              let jsonResult = response.data;
              if (jsonResult.state === 20000) {
                this.$message({
                  message: '添加人员成功！',
                  type: 'success'
                });
                this.resetForm(formName);
              } else {
                this.$alert(jsonResult.message, '操作失败', {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
              }
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    }
  }
</script>

